package cn.edu.jlu.community.disuse;

import android.util.Log;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by bob on 2015/3/10.
 */
public class DataLoader {
    public static List<Map<String, Object>> bbs_list ;
    public static List<Map<String, Object>> news_list ;
    public static List<Map<String, Object>> notification_list ;

    public static void getBBS(){
        Log.i("aaa", "getting community nofification data") ;
        bbs_list = new ArrayList<Map<String, Object>>();

        AVQuery<AVObject> query = new AVQuery<AVObject>("Post");
        query.whereEqualTo("category", 1);           //bbs , category 1 .
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> avObjects, AVException e) {
                if( e == null ) {

                    Log.i("aaa","finding posts in background") ;
                    for (AVObject avObject : avObjects) {
                        Map<String, Object> map = new HashMap<String, Object>();
                        map.put("title",avObject.get("title"));
                        map.put("objID",avObject.getObjectId()) ;
                        map.put("author","admin") ;
                        map.put("content",avObject.get("content")) ;
                        map.put("comment_count",avObject.get("commentCount")) ;

                        Log.i("aaa",avObject.get("title")+" "+avObject.get("writer")+avObject.get("content")) ;
                        bbs_list.add(map);
                    }
                }else{
                    Log.i("aaa",e.getMessage()) ;
                    //Toast.makeText(Dataloader.this, "find notification in background exception" + e.getMessage(), Toast.LENGTH_LONG).show() ;
                }
            }
        });
    }

    public static void getNews(){
        Log.i("aaa", "getting community nofification data") ;
        news_list = new ArrayList<Map<String, Object>>();

        AVQuery<AVObject> query = new AVQuery<AVObject>("Post");
        query.whereEqualTo("category", 3);           //社区动态 , category 3 .
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> avObjects, AVException e) {
                if( e == null ) {

                    Log.i("aaa","finding posts in background") ;
                    for (AVObject avObject : avObjects) {
                        Map<String, Object> map = new HashMap<String, Object>();
                        map.put("title",avObject.get("title"));
                        map.put("objID",avObject.getObjectId()) ;
                        map.put("author","admin") ;
                        map.put("content",avObject.get("content")) ;
                        map.put("comment_count",avObject.get("commentCount")) ;

                        Log.i("aaa",avObject.get("title")+" "+avObject.get("writer")+avObject.get("content")) ;
                        news_list.add(map);
                    }
                }else{
                    Log.i("aaa",e.getMessage()) ;
                    //Toast.makeText(Dataloader.this, "find notification in background exception" + e.getMessage(), Toast.LENGTH_LONG).show() ;
                }
            }
        });
    }

    public static void getNotification() {
        Log.i("aaa","getting community nofification data") ;
        notification_list = new ArrayList<Map<String, Object>>();

        AVQuery<AVObject> query = new AVQuery<AVObject>("Post");
        query.whereEqualTo("category", 2);           //notification , category 2 .
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> avObjects, AVException e) {
                if( e == null ) {

                    Log.i("aaa","finding posts in background") ;
                    for (AVObject avObject : avObjects) {
                        Map<String, Object> map = new HashMap<String, Object>();
                        map.put("title",avObject.get("title"));
                        map.put("objID",avObject.getObjectId()) ;
                        map.put("author","admin") ;
                        map.put("content",avObject.get("content")) ;
                        map.put("comment_count",avObject.get("commentCount")) ;

                        Log.i("aaa",avObject.get("title")+" "+avObject.get("writer")+avObject.get("content")) ;
                        notification_list.add(map);
                    }
                }else{
                    Log.i("aaa",e.getMessage()) ;
                    //Toast.makeText(CommunityNotification.this , "find notification in background exception" + e.getMessage() , Toast.LENGTH_LONG).show() ;
                }
            }
        });
    }


}
